/**
 * 充值管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="margin-t10">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>充值管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
   
    <div class="boxBlock"> 
      <div class="serachTop"> 
        <el-form :inline="true" :model="formInline" class="user-search">
            <el-form-item  >
              <el-select
                size="small"
                v-model="formInline.status"
                placeholder="请选择审核状态"
                clearable
              >
                <el-option label="请选择审核状态" value=""></el-option>
                <el-option
                  v-for="type in statusData"
                  :label="type.key"
                  :value="type.value"
                  :key="type.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="search"
                >搜索</el-button
              >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                @click="handleEdit()"
                >充值</el-button
              >
              <!-- <el-button
                size="small"
                type="primary"
                icon="el-icon-bank-card"
                @click="handleWith()"
              >
                提现
              </el-button> -->
            </el-form-item>
        </el-form>
        <div>
          <el-button size="small" type="primary" icon="el-icon-download" @click="exportEx()">
             导出
          </el-button> 
        </div>
       
      </div>
     
    <!--列表-->
    <div class="ra_box"> 
      <el-table
        size="small"
        :data="listData"
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        style="width: 100%"
      >
        <el-table-column prop="orderNo" label="订单编号" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="usd" label="充值金额" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ b(scope.row.usd) }}
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="到账金额" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ b(scope.row.amount) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="充值时间"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ timestampToTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="审核状态">
          <template slot-scope="scope">
            {{ statusData[scope.row.status].key }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="remark" label="备注" show-overflow-tooltip>
      </el-table-column> -->
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <el-button size="mini" plain @click="handleEdit(scope.$index, scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 充值界面 -->

    </div>

   
    <el-dialog
      :close-on-click-modal="false"
      title="充值"
      :visible.sync="addFormVisible"
      width="800px"
      @close="closeDialog"
    > 
      <div>

        <el-alert title="不接受任何个人账户转账" type="error" style="margin-bottom: 20px;" /> 
        <div class="dialogItem"> 
          <span>账户信息</span>
        </div>
        <el-form  
            :model="addForm"
            :rules="rulesAdd"
            ref="addForm"  
            label-width="140px">
          <el-form-item label="账户信息：" style="margin-top:20px"> HT International Trade Service Limited</el-form-item>
          <el-form-item label="银行账号："> 79821010003964</el-form-item>
          <el-form-item label="SWIFT code/BIC："> DHBKHKHH</el-form-item>
          <el-form-item label="银行代码："> 016</el-form-item>
          <el-form-item label="分行代码："> 478</el-form-item>
          <el-form-item label="银行所在地："> 中国香港</el-form-item>  
          <el-form-item label="金额" prop="rechargeAmount">
              <el-input
                size="small"
                v-model="addForm.rechargeAmount"
                auto-complete="off"
                placeholder="请输入充值金额xxx"
                style="width: 300px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="充值凭证" prop="receipt">
              <el-upload
                action="#"
                ref="upload"
                list-type="picture-card"
                :show-file-list="false"
                :auto-upload="false"
                :on-change="handleChange"
                class="margin-t35 margin-b35 "
              >
                <img
                  v-if="addForm.receipt"
                  :src="addForm.receiptUrl"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="备注" prop="remark" class="wid-7">
              <el-input
                size="small"
                v-model.trim="addForm.remark"
                auto-complete="off"
                type="textarea"
                :rows="5"
                placeholder="请输入备注"
              ></el-input>
            </el-form-item>
        </el-form>

      </div>
      <el-row :gutter="20">
        <el-col :span="12">

          <!-- <i class="el-icon-bank-card text-black font20 font-w6">账户信息：</i>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">账户名称</div>
            <div class="font16 margin-t10">
              HT International Trade Service Limited
            </div>
          </div>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">银行账号</div>
            <div class="font16 margin-t10">79821010003964</div>
          </div>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">SWIFT code/BIC</div>
            <div class="font16 margin-t10">DHBKHKHH</div>
          </div>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">银行代码</div>
            <div class="font16 margin-t10">016</div>
          </div>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">分行代码</div>
            <div class="font16 margin-t10">478</div>
          </div>
          <div class="margin-t20">
            <div class="font16 text-black font-w6">银行所在地</div>
            <div class="font16 margin-t10">中国香港</div>
          </div> -->


          <!-- <el-form
            :model="addForm"
            :rules="rulesAdd"
            ref="addForm"
            label-width="100px"
            label-position="left"
            style="50%"
          >
            
          </el-form> -->
        </el-col>
        <el-col :span="12">
         
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog()">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="submitFormAdd('addForm')"
          :disabled="disabled"
          >提交</el-button
        >
      </div>
    </el-dialog>
    <!-- 查看界面 -->
    <el-dialog
      title="查看"
      :close-on-click-modal="false"
      :visible.sync="editFormVisible"
      width="600px"
      @close="closeDialog"
      v-if="editForm"
    >
      <el-form :model="editForm" ref="editForm" class="myeditForm">
        <!-- class="wid-4" -->
        <el-form-item label="充值单号" prop="orderNo" >
          {{ editForm.orderNo }}
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          {{ statusData[editForm.status].key }}
        </el-form-item>
        <el-form-item label="充值金额" prop="usd" >
          {{ editForm.usd }}
        </el-form-item>
        <el-form-item label="到账金额" prop="amount">
          {{ b(editForm.amount) }}
        </el-form-item>
        <el-form-item label="充值时间" prop="createTime">
          {{ timestampToTime(editForm.createTime) }}
        </el-form-item>
        <el-form-item label="审核备注" prop="auditResult">
          {{ editForm.auditResult }}
        </el-form-item>
        <el-form-item
          label="充值凭证"
          prop="receipt"
         
          v-if="editForm.receipt"
        >
          <el-image
            v-if="editForm.receipt"
            :src="baseURL + `/profile` + editForm.receipt.fileStoragePath"
            :preview-src-list="[
              baseURL + `/profile` + editForm.receipt.fileStoragePath,
            ]"
          ></el-image>
        </el-form-item>
        <el-form-item label="充值备注" prop="remark" >
          {{ editForm.remark }}
        </el-form-item>
      </el-form>
    </el-dialog>
    <codeChoose ref="codeRef" :fromType="verType" @setParam="getParam" ></codeChoose>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import codeChoose from '@/components/codeChoose.vue'
import {
  baseURL,
  req,
  service,
  encrypt,
  decrypt,
  reqtoken,
  reqField,
  getUrl,
  reqJson,
  format
} from "../../api/axiosFun";
import {
  setStoreS,
  getStoreS,
  removeStoreS,
  insertCharacter,
  timestampToTime,
  spStr,

} from "../../utils/util";
 
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false,
      addFormVisible: false,
      editFormVisible: false,
      b: format,
      timestampToTime: timestampToTime,
      baseURL: baseURL,
      disabled: false,
      statusData: [
        { key: "待审核", value: 0 },
        { key: "通过", value: 1 },
        { key: "驳回", value: 2 },
      ],
      addForm: {
        rechargeAmount: "",
        remark: "",
        receipt: null,
        receiptUrl: null,
      },
      editForm: "",
      // rules表单验证
      rulesAdd: {
        rechargeAmount: [
          { required: true, message: "充值金额(USD)", trigger: "blur" },
          {
            pattern: /^[0-9]+(\.\d{1,2})?$/,
            message: "请输入正整数或者2位数小数",
            trigger: "blur",
          },
        ],
        receipt: [
          { required: true, message: "请上传充值凭证", trigger: "blur" },
        ],
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        status: "",
      },
      listData: [],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      headers:'',
      verArry:[],
      verType:'',
      curItem:'', 
      userInfo: null,
      fd:''
    };
  },
  // 注册组件
  components: {
    Pagination,
    codeChoose
  },
  computed: {
    ...mapGetters(["getUser"]),
  },
  watch: {
    getUser(newVal) {
      this.userInfo = JSON.parse(newVal);
    },
  },
  created() {
    if (this.$store.state.isRecharge) {
      this.handleEdit();
      this.$store.commit("isRecharge", false);
    }
    this.getdata(this.formInline);
    this.getInfo();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    getInfo() {
      service
        .get("/api/photon/getUserInfo", {})
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.userInfo = res.data;
            this.$store.commit("user", JSON.stringify(res.data));
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    exportEx() {
      let url = getUrl(this.formInline);
      service
        .post("/api/photon/exportRechargeRecordList" + url, {})
        .then((res) => {
          this.loading = false; 
          if (res.code == 0) {
            window.open(
              baseURL + "/common/download?fileName=" + res.msg,
              "_blank"
            );
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    getdata(parameter) {
      let url = getUrl(parameter);
      service
        .get("/api/photon/getRechargeRecordList" + url)
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.listData = res.rows;
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.total;
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    handleChange(files, fileList) {
      const isJPG = files.raw.type == "image/jpeg" || "image/png";
      if (!isJPG) {
        this.$message.error("上传图片格式不符合！");
        return;
      }
      if (files.size > 1024 * 1024 * 3) {
        this.$message.error("上传图片不能超过3M！");
        return;
      }
      this.addForm.receiptUrl = URL.createObjectURL(files.raw);
      this.addForm.receipt = files.raw;
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit(index, row) {
      if (row != undefined && row != "undefined") {
        this.editFormVisible = true;
        this.editForm = row;
      } else {
        this.addFormVisible = true;
      }
    },
    handleWith() {
      this.$alert("请联系管理员", "提现", {
        confirmButtonText: "确定",
        callback: () => {},
      });
    },

    getParam(val,type){  
      this.headers = val ;
      this.editSaveAuth() 
    }, 


    editSaveAuth(){

      let url =
        "?rechargeAmount=" +
        this.addForm.rechargeAmount +
        "&remark=" +
        this.addForm.remark;

      reqJson('post',"/api/photon/recharge" + url, this.fd,{
        headers:{
          ...this.headers
        }
      }).then((res) => {
          this.disabled = false;
          if (res.code == 0) {
            this.$message.success("提交成功，请等待审核...");
            this.getdata(this.formInline);
            this.closeDialog();
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },


    submitFormAdd(form) { 

      this.$refs[form].validate((valid) => {
        if (valid) {
          const fd = new FormData();
          this.disabled = true;
          fd.append("receipt", this.addForm.receipt);
     
          this.fd = fd;
          if(this.userInfo&&this.userInfo.verifyApi){
            this.verArry =  this.userInfo.verifyApi.split(',');
            if(this.verArry.includes('/api/photon/recharge')){
              this.$refs.codeRef.openDialog(); 
            }else{
              this.editSaveAuth();
            }
          }else{
            this.editSaveAuth();
          }


          let url =
            "?rechargeAmount=" +
            this.addForm.rechargeAmount +
            "&remark=" +
            this.addForm.remark;
          // service
          //   .post("/api/photon/recharge" + url, fd)
          //   .then((res) => {
          //     this.disabled = false;
          //     if (res.code == 0) {
          //       this.$message.success("提交成功，请等待审核...");
          //       this.getdata(this.formInline);
          //       this.closeDialog();
          //     } else {
          //       this.$message({
          //         type: "info",
          //         message: spStr(res.msg, "PhotonPay", ""),
          //       });
          //     }
          //   })
          //   .catch((err) => {
          //     this.$message.error(spStr(err, "PhotonPay", ""));
          //   });
        } else {
          return false;
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
      this.addFormVisible = false;
      this.$refs.editForm.resetFields();
      this.$refs.addForm.resetFields();
    },
  },
};
</script>

<style scoped>


.boxBlock{ 
  background: #fff;
  border-radius: 8px;
  box-shadow: 0px 2px 4px 0px rgba(189, 205, 221, 0.58);
  padding: 16px 20px; 
  margin:20px 0;
}
/* .serachTop{
  display: flex;
  justify-content: space-between;
  align-items: center;
} */

.boxBlock .card_title{
  padding: 10px 0;
  /* border-left: 4px solid rgb(46, 48, 148); */
  position: relative;
  font-size: 15px;
  font-weight: bold;
}
.boxBlock .card_title::after{
  position: absolute;
  width: 4px;
  height: 20px;
  left: -20px;
  content: '';
  background: rgb(46, 48, 148);
}




.user-search {
  margin-top: 20px;
}
.user-search .el-form-item .el-form-item__content {
  line-height: normal;
}
.userRole {
  width: 100%;
}
.myeditForm .el-form-item {
  /* display: inline-block; */
  margin-left: 5%;
}
.el-upload .avatar {
  margin-left: -15px;
  margin-top: -15px;
  width: 180px;
  height: 180px;
  display: block;
}
.el-image {
  width: 180px;
  height: 180px;
}
</style>

 
 